#include "types.h"
#include "stat.h"
#include "user.h"
#include "signal.h"
#include "barrier.c"
//#include "fssp.h"

//struct binary_semaphore semaphore;
struct barrier bar;
struct barrier bar2;
int banana[10];

void print_num(void *num);
void work();


void work2(void *arg)
{
	int i = banana[uthread_self()%10];
	barrier_pass(&bar);
	banana[(uthread_self()-1)%10] = i+1;
	barrier_pass(&bar2);
}

int main(void)
{
  int i;


  uthread_init();
  barrier_init(&bar, 10);
  printf(1, "\ngot here1\n");
  barrier_init(&bar2, 11);
  printf(1, "\ngot here2\n");
  //binary_semaphore_init(&semaphore, 1);
  for (i = 0; i < 10; i++)
  {
	  uthread_create(work2, 0);
  }
  printf(1, "\ngot here3\n");
  //printf(1,"\ntook over\n");

  barrier_pass(&bar2);
  printf(1, "\ngot here4\n");
  for (i = 0; i < 10; i++)
	  printf(1, "%d\n",banana[i]);
  printf(1, "\ngot here5\n");
  uthread_exit();

  exit();
}

void work()
{
	int i;
	int tid = uthread_self();
	for (i = 0; i < 10; i++)
		printf(1,"Thread %d's print number %d\n", tid,i);
	//binary_semaphore_down(&semaphore);
	for (i = 10; i < 20; i++)
		printf(1,"Thread %d's print number %d\n", tid,i);
	//binary_semaphore_up(&semaphore);
}

void print_num(void *num)
{
	int *i = num;
    printf(1,"\nIf we're all good num is: %d\n", *i);
}
